package class_and_object.exception;

import java.util.ArrayList;
import java.util.Scanner;

public class TestSimpleCircle {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<Circle> arrCircle = new ArrayList<>();
        System.out.println("请输入半径：");
        double radius = in.nextDouble();
        while (radius != 0){
            try {
                arrCircle.add(new Circle(radius));
                System.out.println("创建成功！");
            }catch (InvlidRadiusException e){
                System.out.println(e.getMessage());
            }

            System.out.println("请输入半径：");
            radius = in.nextDouble();
        }

        System.out.println("----------------");
        for(Circle c : arrCircle){
            System.out.println("半径："+c.getR());
        }
    }
}

class Circle{
    double r = 1;

    // 无参构造
    public Circle() throws InvlidRadiusException{
        this(1.0);
    }

    // 有参构造
    public Circle(double r) throws InvlidRadiusException{
        setNewR(r);
    }

    // 得到圆半径
    public double getR() {
        return r;
    }

    // 得到圆面积
    public double getArea(){
        return 3.1415926 * r * r;
    }

    // 得到圆周长
    public double getPerimeter(){
        return 2 * 3.1415926 * r;
    }

    // 设置圆半径
    public double setNewR(double newR) throws InvlidRadiusException {
        if(newR < 0){
            throw new InvlidRadiusException("半径不能小于0！");
        }
        r=newR;
        return r;
    }
}

// 自定义的一个异常类
class InvlidRadiusException extends Exception{
    public InvlidRadiusException() {
        super();
    }

    public InvlidRadiusException(String message) {
        super(message);
    }
}
